﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _5_2_Function
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = { 26, 1007, 956, 705, 574, 371, 416, 517, 1111, 0, 1, 0, 26, 705, };
            int[] b = { 994, 631, 772, 201, 262, 763, 1000, 781, 7 };

            //选择排序实现
            Console.Write("原数组");
            PrintArray(a);
            Console.Write("排序后数组");
            PrintArray(SelectSort(a));
            Console.Write("原数组");
            PrintArray(a);
            Console.ReadKey();

            //字符串排序
            SortString();
            Console.ReadKey();


            //剔除数组相同数字
            PrintArray(ArrayOutlier(a));
            Console.ReadKey();

            //返回两个数组的最大公约数组
            int[] ret = GetGCDArray(a, b);
            PrintArray(ret);
            Console.ReadKey();
        }

        private static int[] SelectSort(int[] array)
        {
            #region Array Copy
            //1
            int[] ret = new int[array.Length];
            //for (int i = 0; i < array.Length; i++)
            //{
            //   ret[i] = array[i];
            //}
            //2
            array.CopyTo(ret, 0);
            //3
            //Array.Copy(array, ret, ret.Length);
            //4 
            //ret = (int[])array.Clone();
            //5 error
            //ret = array;
            #endregion

            int min = 0;
            int temp = 0;
            for (int i = 0; i < ret.Length; i++)
            {
                min = i;
                for (int j = i; j < ret.Length; j++)
                {
                    if (ret[min] > ret[j])
                        min = j;
                }
                temp = ret[min];
                ret[min] = ret[i];
                ret[i] = temp;
            }
            return ret;
        }

        private static void SortString()
        {
            string con = "";
            do
            {
                con = "";

                string[] stringArray = new string[10];
                int len = 0;
                do
                {
                    Console.WriteLine("请输入字符串：");
                    string s = Console.ReadLine();
                    stringArray[len] = s;
                    len++;
                    Console.WriteLine("是否继续输入字符串？（Y/N）");
                    con = Console.ReadLine();
                } while ((con == "Y" || con == "y") && len<1024);

                con = "";

                Array.Sort(stringArray);

                PrintArray(stringArray);

                Console.WriteLine("是否继续？（Y/N）");
                con = Console.ReadLine();
            } while (con == "Y" || con == "y");


            Console.ReadKey();
        }

        private static int[] ArrayOutlier(int[] a)
        {
            int[] ret;
            Array.Sort(a);
            int len = 1, temp = a[0];
            for (int i = 0; i < a.Length; i++)
            {
                if (temp != a[i])
                {
                    len++;
                    temp = a[i];
                }
            }
            ret = new int[len];
            len = 1;
            temp = a[0];
            ret[0] = a[0];
            for (int i = 0; i < a.Length; i++)
            {
                if (temp != a[i])
                {
                    len++;
                    temp = a[i];
                    ret[len-1] = a[i];
                }
            }
            return ret;
        }

        private static void PrintArray<T>(T[] arr)
        {
            Console.Write("Array: ");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }
            Console.WriteLine();
        }

        private static void PrintArray(int[] arr)
        {
            Console.Write("Array: ");
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + "  ");
            }
            Console.WriteLine();
        }

        private static int GetGCD(int a, int b)
        {
            int min = a > b ? b : a;
            int max = a < b ? b : a;
            for (int i = min; i >0; i--)
            {
                if (max % i == 0 && min % i == 0)
                {
                    return i;
                }
            }
            return 1;
        }

        private static int[] GetGCDArray(int[] arr1, int[] arr2)
        {
            int[] ret= new int[arr1.Length<arr2.Length? arr1.Length:arr2.Length];
            for (int i = 0; i < arr1.Length && i<arr2.Length; i++)
            {
                ret[i] = GetGCD(arr1[i], arr2[i]);
            }
            return ret;
        }
    }
}
